<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\ModelStatistic\NumberOfLevels;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Form;

use App\ModelStatistic\RemainByNewUserLoginModel;
use App\ModelStatistic\RemainByNewAccount;
use App\ModelStatistic\RetainWeek;
use App\ModelStatistic\StatisticCommon;
use App\Models\Account\DurationDeinc;
use App\Models\Area;
use App\ModelStatistic\RetainAccountTemp;
use App\ModelStatistic\IncreaseNewPlayer;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Admin\Extra\Search\Html;

//用户统计
class RetainAccountController extends Controller {

    public function anyStAccountTemp() {
        return Admin::content(function (Content $content) use($data){
            $area_id = Input::get('area');
            $area_all = \App\Models\Area::areaSelectData(0);
            
		    $content->header( " retain earnings group by Account [{$area_all[$area_id]}]");
		    $content->description('statisitc account remain');

            // 添加面包屑导航 since v1.5.7
            $content->breadcrumb(
                ['text' => '首页', 'url' => '/'],
                ['text' => '存留', 'url' => '/admin/userst/remain']
            );

            $qd['cond']['start'] = $start = Input::get('start');
            $qd['cond']['end'] = $end = Input::get('end');

            $append_button = StatisticCommon::RetainHomeButton($area_id, 'day');
            $req_all = Input::all();
            
            array_unshift($append_button, Input::get('valid', 1)==0 ? Html::button('valid', \App\Service\Common::build_url_query(url()->current(), array_merge($req_all, ['valid'=>1])), 'btn btn-goto btn-info')
            :Html::button('valid', \App\Service\Common::build_url_query(url()->current(), array_merge($req_all, ['valid'=>0])), 'btn btn-goto btn-default') );
            array_unshift($append_button, Html::button('chart', '/admin/userst/remain?func=chart&start='.$start.'&end='.$end,'btn-goto'));
           
            
            $append_button[] = Input::get('day10', 0)==0 ? Html::button('10-20Day', \App\Service\Common::build_url_query(url()->current(), array_merge($req_all, ['day10'=>1])), 'btn btn-goto btn-info')
                :Html::button('10-20Day', \App\Service\Common::build_url_query(url()->current(), array_merge($req_all, ['day10'=>0])), 'btn btn-goto btn-default');

            $append_button[] = Input::get('day30', 0) ==0 ? Html::button('20-30Day', \App\Service\Common::build_url_query(url()->current(), array_merge($req_all, ['day30'=>1])), 'btn btn-goto btn-info')
                :Html::button('20-30Day', \App\Service\Common::build_url_query(url()->current(), array_merge($req_all, ['day30'=>0])), 'btn btn-goto btn-default');
		    $content->row(Html::append($append_button));		

			$grid = Admin::grid(RetainAccountTemp::class, function(Grid $grid) use($qd, $req_all){
                $colsour_func = function($obj, $value){
                    $obj->retain_num = $obj->retain_num ?: 1;
                    $rate_txt = StatisticCommon::DisplayRetainNagtive($value, $obj->retain_num, true);
                    if (intval($obj->retain_time) . '' == $obj->retain_time) {
                        return $rate_txt;
                    } else {
                        return '<div style="background:#000;color:#eee;">' . $rate_txt . '</div>';
                    }
                    // $right_value = $value > 0 ?: $value * -1;
                    // return Html::processerBar(round($right_value/$this->retain_num * 100, 2) . '%', $rate_txt);
                };
            
                //$grid->paginate(15);
			    // $grid->id('id');
			    $grid->retain_time('date')->style('border:1px solid #ccc;padding:0; text-align:center;')->display(function($value){
                    if (intval($value) . '' == $value) {
                        return '<div style="width:80px;">'.date("Y-m-d", $value). '</div>';
                    } else {
                        return '<div style="width:100%;background:#000; color:#eee; line-height:50px;">'.$value . '</div>';
                    }
                });
                $grid->retain_fuck('RATE')->style('border:1px solid #ccc;padding:0;')->display(function($value){
                    return $value;
                    //return \App\Models\Account\PlayerActModel::getFollowAccountCond( $this, 0, StatisticCommon::DisplayRetainNagtive($value, $this->retain_num) );
                });
			    $grid->retain_num('Day 1')->style('border:1px solid #ccc;padding:0;')->display(function($value){
                    return \App\Models\Account\PlayerActModel::getFollowAccountCond( $this, 0, StatisticCommon::DisplayRetainNagtive($value, $this->retain_num) );
                });

                $persent = true;
                for($i=1; $i<=30; $i++) {
                    $fld = 'day'.$i;
                    $ii = $i + 1;
                    if (!$req_all['day10'] && $i >= 10 && $i <20){
                        continue;
                    }
                    if (!$req_all['day30'] && $i >= 20 && $i <= 30){
                        continue;
                    }
			        $grid->$fld("Day {$ii}")->style('border:1px solid #ccc;padding:0;')->display(function ($value)use($colsour_func, $i) {
                        return \App\Models\Account\PlayerActModel::getFollowAccountCond($this, $i, $colsour_func($this, $value));
                    });
                }
			    // $grid->day7('week')->display($colsour_func);
			    // //$grid->week2('第二周');
			    // $grid->day14('half month')->display($colsour_func);
			    // $grid->day30('month')->display($colsour_func);

			    $grid->filter(function ($filter) {


			        // 设置created_at字段的范围查询
			        $filter->between('retain_time', '日期')->datetime();
                    $filter->equal('area')->select(\App\Models\Area::areaSelectData(1));
                    $filter->equal('mmodel')->select(\App\ModelStatistic\StatisticCommon::getMobieModel());
                    //
                    // $filter->execute();
                    // $filter->conditions();
                    $filter->where = $this->model;
                    $filter->disableIdFilter();
			    });

                $grid->actions(function($actions){
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="'.url('#'). $actions->row->account .'"><i class="glyphicon glyphicon-eye-open"></i></a>');
                });
                $grid->disableCreateButton();
                //$grid->disableExport();
                $grid->disableActions();
                $grid->disableRowSelector();
			});
       
		    $content->row($grid->render());	


        });           
    }
}